package com.htjx.mall.commons.models.product;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;


/**
 * 
 * @author Administrator
 *
 */
public class IntroduceAPIs {
	
	static public void saveXml(String fileName, Introduce introduce, int index) {
		if(StringUtils.isBlank(fileName)) {
			return;
		}
		SAXReader saxReader = new SAXReader();
		Document document = null;
		Element root = null;
		File file = FileUtils.getFile(fileName);
		if(file.exists()) {
			try {
				document = saxReader.read(new File(fileName));
				root = document.getRootElement();
			} catch (DocumentException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} else {
			file = new File(fileName);
			document = DocumentHelper.createDocument(); 
			root = document.addElement("root");
		}
		
		Element elem = DocumentHelper.createElement("introduce");  
		if(introduce.isPlain()) {
			elem.addAttribute("type", "text");
		} else {
			elem.addAttribute("type", "image");
		}
		elem.setText(introduce.getContent()); 
		@SuppressWarnings("unchecked")
		List<Element> elementList = root.elements();
		int size = elementList.size();
		if(index >= 0 && index < size) {
			elementList.add(index, elem); 
		} else {
			elementList.add(elem);
		}
		
		//Element content = root.addElement("introduce");
		//content.setText(introduce.getContent());
        
        try {    
            Writer fileWriter=new FileWriter(fileName);    
            XMLWriter xmlWriter=new XMLWriter(fileWriter);    
            xmlWriter.write(document);   //写入文件中 
            xmlWriter.close();   
            } catch (IOException e) {    
                System.out.println(e.getMessage());    
            }    
	}
	
	static public void xmlDeleteElement(String fileName, int index) {
		if(StringUtils.isBlank(fileName)) {
			return;
		}
		SAXReader saxReader = new SAXReader();
		Document document = null;
		Element root = null;
		File file = FileUtils.getFile(fileName);
		if(file.exists()) {
			try {
				document = saxReader.read(new File(fileName));
				root = document.getRootElement();
			} catch (DocumentException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} else {
			return;
		}
		
		@SuppressWarnings("unchecked")
		List<Element> elementList = root.elements();
		int size = elementList.size();
		if(index >= 0 && index < size) {
			elementList.remove(index);
		} else {
			return;
		}
        
        try {    
            Writer fileWriter=new FileWriter(fileName);    
            XMLWriter xmlWriter=new XMLWriter(fileWriter);    
            xmlWriter.write(document);   //写入文件中 
            xmlWriter.close();   
            } catch (IOException e) {    
                System.out.println(e.getMessage());    
            }    
	}
	
	static public List<Introduce> parseXml(String fileName) {
		if(StringUtils.isBlank(fileName)) {
			return null;
		}
		List<Introduce> introduceList = new ArrayList<Introduce>();
		File file = FileUtils.getFile(fileName);
		if(!file.exists()) {
			return introduceList;
		}
		
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new File(fileName));
			Element root = document.getRootElement();
			@SuppressWarnings("rawtypes")
			Iterator iter = root.elementIterator("introduce");
			while (iter.hasNext()) {
				Introduce introduce = new Introduce();
				Element element = (Element) iter.next();
				if("image".equals(element.attributeValue("type"))) {
					introduce.setPlain(false);
				} else {
					introduce.setPlain(true);
				}
				introduce.setColor(element.attributeValue("color"));
				introduce.setContent(element.getText());
				introduceList.add(introduce);
			}
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return introduceList;
	}
}
